/**
 * 
 */
package com.estock.web.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.estock.common.model.security.Stock;
import com.estock.common.service.impl.BaseServiceImpl;
import com.estock.web.dao.StockDao;
import com.estock.web.service.StockService;

/**
 * @author richard
 *
 */
@Service
public class StockServiceImpl extends BaseServiceImpl implements StockService {
	
	@Resource
	private StockDao stockDao;
	
	public StockDao getStockDao() {
		return stockDao;
	}

	@Override
	public List<Stock> findAllStocksByType(int type, int currentPage, int itemsPerPage) {
		return getStockDao().findAllStocksByType(type, currentPage, itemsPerPage);
	}
	

	@Override
	public Stock findStockByCode(String code) {
		return getStockDao().findStockByCode(code);
	}

	@Override
	public void saveOrUpdate(Stock stock) {
		getStockDao().saveOrUpdate(stock);
	}

	@Override
	public Stock findById(Long id) {
		Stock stock = getStockDao().findById(id);
		// fix LazyInitializationException error
//		for (StockCategory sc: stock.getCategories()) {
//			sc.getName();
//		}
		return stock;
	}

	@Override
	public List<Stock> findStocksByCategoryId(int stockCategoryId) {
		return getStockDao().findStocksByCategoryId(stockCategoryId);
	}

	@Override
	public int getStockNumByType(int type) throws Exception {
		return getStockDao().getStockNumByType(type);
	}
	
	@Override
	public void deleteStockById(Long id) {
		getStockDao().deleteById(id);
	}
	
	
	////////Self-Select stock related

	@Override
	public int getSelfSelectNum() throws Exception {
		return getStockDao().getSelfSelectNum();
	}

	@Override
	public List<Stock> findAllSelfSelects(int currentPage, int itemsPerPage)
			throws Exception {
		return getStockDao().findAllSelfSelects(currentPage, itemsPerPage);
	}

	@Override
	public Stock findSelfSelectByCode(String code) throws Exception {
		return getStockDao().findSelfSelectByCode(code);
	}

	@Override
	public void saveSelfSelect(Stock entity) throws Exception {
		getStockDao().saveOrUpdate(entity);
	}

	@Override
	public void updateLatestPrice(String code, float latestPrice) {
		getStockDao().updateLatestPrice(code, latestPrice);
	}

	@Override
	public void deleteSelfSelectById(Long id) throws Exception {
		getStockDao().deleteSelfSelectById(id);
	}

}
